package site.lanmushan.framework.mapper.wrapper;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitor;
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import site.lanmushan.framework.mapper.params.SlashAndExpression;
import site.lanmushan.framework.mapper.params.SlashExpression;
import site.lanmushan.framework.mapper.params.SlashExpressionValue;
import site.lanmushan.framework.mapper.params.WhereQueryParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class QueryParamWrapper extends DefaultWrapper {

    public QueryWrapper setQueryParam(List<WhereQueryParam> list) {
        SlashExpression slashExpression = new SlashAndExpression();
        for (WhereQueryParam param : list) {
            Expression expression = null;
            switch (param.getOperate()) {
                case "llkie": {
                    ComparisonOperator comparisonOperator = new ComparisonOperator(param.getOperate()) {

                        @Override
                        public void accept(ExpressionVisitor expressionVisitor) {

                        }

                    };
                    comparisonOperator.setLeftExpression(createColumn(param.getColumn()));
                    comparisonOperator.setRightExpression(new SlashExpressionValue("%" + param.getValue()));
                    expression = comparisonOperator;
                    break;
                }
                case "rlike": {
                    ComparisonOperator comparisonOperator = new ComparisonOperator(param.getOperate()) {

                        @Override
                        public void accept(ExpressionVisitor expressionVisitor) {

                        }

                    };
                    comparisonOperator.setLeftExpression(createColumn(param.getColumn()));
                    comparisonOperator.setRightExpression(new SlashExpressionValue(param.getValue() + "%"));
                    expression = comparisonOperator;
                    break;
                }
                case "like": {
                    ComparisonOperator comparisonOperator = new ComparisonOperator(param.getOperate()) {

                        @Override
                        public void accept(ExpressionVisitor expressionVisitor) {

                        }

                    };
                    comparisonOperator.setLeftExpression(createColumn(param.getColumn()));
                    comparisonOperator.setRightExpression(new SlashExpressionValue("%" + param.getValue() + "%"));
                    expression = comparisonOperator;
                    break;
                }
                case "in": {
                    List values = new ArrayList<>();
                    if (param.getValue() instanceof String) {
                        values = Arrays.stream(((String) param.getValue()).split(",")).collect(Collectors.toList());
                    }
                    InExpression inExpression = new InExpression();
                    inExpression.setLeftExpression(createColumn(param.getColumn()));
                    List<Expression> expressions = new ArrayList<>();
                    for (Object value : values) {
                        expressions.add(new SlashExpressionValue(value));
                    }
                    MultiExpressionList multiExpressionList = new MultiExpressionList();
                    multiExpressionList.addExpressionList(expressions);
                    inExpression.setRightItemsList(multiExpressionList);
                    expression=inExpression;
                    break;
                }
                default: {
                    ComparisonOperator comparisonOperator = new ComparisonOperator(param.getOperate()) {

                        @Override
                        public void accept(ExpressionVisitor expressionVisitor) {

                        }

                    };
                    comparisonOperator.setLeftExpression(createColumn(param.getColumn()));
                    comparisonOperator.setRightExpression(new SlashExpressionValue(param.getValue()));
                    expression = comparisonOperator;
                }
            }

            slashExpression.addExpression(expression);
        }
        this.andExpression(slashExpression);
        return this;
    }
}
